#ifndef __DRV_CC1101_H__
#define __DRV_CC1101_H__

#include "stdint.h"
#include "drv_spi.h"
#include "delay.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"

// 寄存器
#define CC1101_IOCFG2       0x00
#define CC1101_IOCFG1       0x01
#define CC1101_IOCFG0       0x02
#define CC1101_FIFOTHR      0x03
#define CC1101_SYNC1        0x04
#define CC1101_SYNC0        0x05
#define CC1101_PKTLEN       0x06
#define CC1101_PKTCTRL1     0x07
#define CC1101_PKTCTRL0     0x08
#define CC1101_ADDR         0x09
#define CC1101_CHANNR       0x0A
#define CC1101_FSCTRL1      0x0B
#define CC1101_FSCTRL0      0x0C
#define CC1101_FREQ2        0x0D
#define CC1101_FREQ1        0x0E
#define CC1101_FREQ0        0x0F
#define CC1101_MDMCFG4      0x10
#define CC1101_MDMCFG3      0x11
#define CC1101_MDMCFG2      0x12
#define CC1101_MDMCFG1      0x13
#define CC1101_MDMCFG0      0x14
#define CC1101_DEVIATN      0x15
#define CC1101_MCSM2        0x16
#define CC1101_MCSM1        0x17
#define CC1101_MCSM0        0x18
#define CC1101_FOCCFG       0x19
#define CC1101_BSCFG        0x1A
#define CC1101_AGCCTRL2     0x1B
#define CC1101_AGCCTRL1     0x1C
#define CC1101_AGCCTRL0     0x1D
#define CC1101_WOREVT1      0x1E
#define CC1101_WOREVT0      0x1F
#define CC1101_WORCTRL      0x20
#define CC1101_FREND1       0x21
#define CC1101_FREND0       0x22
#define CC1101_FSCAL3       0x23
#define CC1101_FSCAL2       0x24
#define CC1101_FSCAL1       0x25
#define CC1101_FSCAL0       0x26
#define CC1101_RCCTRL1      0x27
#define CC1101_RCCTRL0      0x28
#define CC1101_FSTEST       0x29
#define CC1101_PTEST        0x2A
#define CC1101_AGCTEST      0x2B
#define CC1101_TEST2        0x2C
#define CC1101_TEST1        0x2D
#define CC1101_TEST0        0x2E

// 命令
#define CC1101_SRES         0x30
#define CC1101_SFSTXON      0x31
#define CC1101_SXOFF        0x32
#define CC1101_SCAL         0x33
#define CC1101_SRX          0x34
#define CC1101_STX          0x35
#define CC1101_SIDLE        0x36 
#define CC1101_SAFC         0x37
#define CC1101_SWOR         0x38
#define CC1101_SPWD         0x39
#define CC1101_SFRX         0x3A
#define CC1101_SFTX         0x3B
#define CC1101_SWORRST      0x3C
#define CC1101_SNOP         0x3D

// 状态只读寄存器
#define CC1101_PARTNUM      0x30
#define CC1101_VERSION      0x31
#define CC1101_FREQEST      0x32
#define CC1101_LQI          0x33
#define CC1101_RSSI         0x34
#define CC1101_MARCSTATE    0x35
#define CC1101_WORTIME1     0x36
#define CC1101_WORTIME0     0x37
#define CC1101_PKTSTATUS    0x38
#define CC1101_VCO_VC_DAC   0x39
#define CC1101_TXBYTES      0x3A
#define CC1101_RXBYTES      0x3B

//MARCSTATE的各种状态值
#define STATE_SLEEP                         0x00
#define STATE_IDLE                          0x01
#define STATE_XOFF                          0x02
#define STATE_VCOON_MC                      0x03
#define STATE_REGON_MC                      0x04
#define STATE_MANCAL                        0x05
#define STATE_VCOON                         0x06
#define STATE_REGON                         0x07
#define STATE_STARTCAL                      0x08
#define STATE_BWBOOST                       0x09
#define STATE_FS_LOCK                       0x0A
#define STATE_IFADCON                       0x0B
#define STATE_ENDCAL                        0x0C
#define STATE_RX                            0x0D
#define STATE_RXEND                         0x0E
#define STATE_RXRST                         0x0F
#define STATE_TXRX_SWITCH                   0x10
#define STATE_RXFIFO_OVERFLOW               0x11
#define STATE_FSTXON                        0x12
#define STATE_TX                            0x13
#define STATE_TX_END                        0x14
#define STATE_RXTX_SWITCH                   0x15
#define STATE_TXFIFO_UNDERFLOW              0x16

// 特殊多字节寄存器
#define CC1101_PATABLE      0x3E
#define CC1101_TXFIFO       0x3F
#define CC1101_RXFIFO       0x3F

//掩码
#define WRITE_BURST         0x40            //写多个
#define READ_SINGLE         0x80            //读单个
#define READ_BURST          0xC0            //读多个
#define BYTES_IN_RXFIFO     0x7F            //接收缓冲区有效字节
#define CRC_OK              0x80            //CRC_OK在载荷最后一字节中的位置
#define BROAD_ALL           0x00            //收所有包
#define BROAD_NO            0x01            //不收所有广播
#define BROAD_0             0x02            //只收地址为0的广播
#define BROAD_0AND255       0x03            //收地址为0和255的广播

#define PA_TABLE                         {0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,}

//CC1101硬件接口定义
#define CC1101_GDO0_GPIO_PORT               GPIOA
#define CC1101_GDO0_GPIO_CLK                RCC_APB2Periph_GPIOG
#define CC1101_GDO0_GPIO_PIN                GPIO_Pin_0

#define CC1101_GDO2_GPIO_PORT               GPIOA
#define CC1101_GDO2_GPIO_CLK                RCC_APB2Periph_GPIOG
#define CC1101_GDO2_GPIO_PIN                GPIO_Pin_1

#define CC1101_CSN_GPIO_PORT                SPI_NSS_GPIO_PORT
#define CC1101_CSN_GPIO_CLK                 SPI_NSS_GPIO_CLK
#define CC1101_CSN_GPIO_PIN                 SPI_NSS_GPIO_PIN



// 口线操作函数定义
#define CC1101_SET_CSN_HIGH( )              spi_set_nss_high( )
#define CC1101_SET_CSN_LOW( )               spi_set_nss_low( )



typedef enum 
{ 
    BROADCAST, 
    ADDRESS_CHECK
} CC1101_TxDataModeType;

//命令
#define CC1101_Rx_Mode                      CC1101_Write_Cmd(CC1101_SRX)
#define CC1101_Tx_Mode                      CC1101_Write_Cmd(CC1101_STX)
#define CC1101_Set_Idle_Mode                CC1101_Write_Cmd(CC1101_SIDLE)
#define CC1101_Set_Address(addr)            CC1101_Write_Reg(CC1101_ADDR,addr);
#define CC1101_Get_Address(addr)            CC1101_Read_Reg(CC1101_ADDR,addr);
#define CC1101_Clear_RxBuffer               CC1101_Write_Cmd(CC1101_SIDLE);CC1101_Write_Cmd(CC1101_SFRX )
#define CC1101_Clear_TxBuffer               CC1101_Write_Cmd(CC1101_SIDLE);CC1101_Write_Cmd(CC1101_SFTX )
#define CC1101_Get_RxCounter                CC1101_Read_Status(CC1101_RXBYTES)&BYTES_IN_RXFIFO

//写命令和寄存器
void CC1101_Write_Cmd( uint8_t Command );
void CC1101_Write_Reg( uint8_t Addr, uint8_t WriteValue );
void CC1101_Write_Multi_Reg( uint8_t Addr, uint8_t *pWriteBuff, uint8_t WriteSize );

//读寄存器
uint8_t CC1101_Read_Reg( uint8_t Addr );
void CC1101_Read_Multi_Reg( uint8_t Addr, uint8_t *pReadBuff, uint8_t ReadSize );

//读状态寄存器
uint8_t CC1101_Read_Status( uint8_t Addr );

void C1101_WOR_Init( void );
void CC1101_Set_Address_Mode(uint8_t AddressMode);
void CC1101_Tx_Packet( uint8_t address, uint8_t TxSize, uint8_t *pTxBuff );
void CC1101_Reset( void );
void CC1101_Init( void );
#endif
